package tzhomestudio.framework.shared.utils;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import tzhomestudio.framework.implementation.main_service.FrameworkMainServiceHelper;

/**
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class XmlUtils {
    public static int getIntContentFromElementByTag(Element eElement, String tag) {
        return getIntContentFromElementByTag(eElement, tag, 0);
    }

    public static int getIntContentFromElementByTag(Element eElement, String tag, int defaultIntegerContent) {
        try {
            return Integer.parseInt(getTextContentFromElementByTag(eElement, tag, "0"));
        } catch (Exception ex) {
            FrameworkMainServiceHelper.logger.printDebug(ex.getMessage(), ex);
        }

        return defaultIntegerContent;
    }

    public static String getTextContentFromElementByTag(Element eElement, String tag) {
        return getTextContentFromElementByTag(eElement, tag, "");
    }

    public static String getTextContentFromElementByTag(Element eElement, String tag, String defaultTextContent) {
        NodeList nodeList = eElement.getElementsByTagName(tag);

        if (nodeList != null && nodeList.getLength() > 0)
            return nodeList.item(0).getTextContent();

        return defaultTextContent;
    }
}
